package com.idp.app.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.idp.app.dto.AppUserDto;
import com.idp.app.dto.AppUserSearchDto;
import com.idp.app.service.AppUserService;
import com.idp.common.base.BaseController;
import com.idp.common.model.LoginInfo;
import com.idp.common.model.PageModel;
import com.idp.common.model.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * APP用户Controller
 * @author King
 */
@Api(tags="APP用户")
@RestController
@RequestMapping("/appUser/")
public class AppUserController extends BaseController {

	@Autowired
	private AppUserService appUserService;

	/**
	 * 分页查询
	 * @param appUserSearchDto
	 * @return
	 */
	@ApiOperation(value="分页查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "页数", paramType = "query", required = true, defaultValue = "1"),
			@ApiImplicitParam(name = "rows", value = "每页行数", paramType = "query", required = true, defaultValue = "10")
	})
	@PostMapping("findByPage")
	public ResultModel<PageModel<AppUserDto>> findByPage(@RequestBody AppUserSearchDto appUserSearchDto) {
		return ResultModel.success(appUserService.findByPage(appUserSearchDto));
	}

	/**
	 * 通过主键查询
	 * @param appUserDto
	 * @return
	 */
	@ApiOperation(value="通过主键查询")
	@ApiOperationSupport(includeParameters = {"appUserDto.id"})
	@PostMapping("getById")
	public ResultModel<AppUserDto> getById(@RequestBody AppUserDto appUserDto) {
		return ResultModel.success(appUserService.getById(appUserDto.getId()));
	}

	/**
	 * 禁用/恢复
	 * @param appUserDto
	 * @return
	 */
	@ApiOperation(value="禁用/恢复")
	@ApiOperationSupport(includeParameters = {"appUserEntity.id", "appUserEntity.status"})
	@PostMapping("disable")
	public ResultModel disable(@RequestBody AppUserDto appUserDto) {
		appUserService.update(appUserDto);
		return ResultModel.success();
	}

	/**
	 * 用户注册
	 * @param appUserDto
	 * @return
	 */
	@ApiOperation("用户注册")
	@ApiOperationSupport(includeParameters = {"appUserDto.uname", "appUserDto.pwd",
			"appUserDto.phone", "appUserDto.code", "appUserDto.ecode"})
	@PostMapping("registerUser")
	public ResultModel<LoginInfo> registerUser(@RequestBody AppUserDto appUserDto) {
		return ResultModel.success(appUserService.registerUser(appUserDto));
	}

	/**
	 * 获取用户信息
	 * @return
	 */
	@ApiOperation("用户信息")
	@PostMapping("getUserInfo")
	public ResultModel<AppUserDto> getUserInfo() {
		return ResultModel.success(appUserService.getUserInfo());
	}

	/**
	 * 修改用户名
	 * @param appUserDto
	 * @return
	 */
	@ApiOperation(value="修改用户名")
	@ApiOperationSupport(includeParameters = {"appUserDto.uname"})
	@PostMapping("updateUsername")
	public ResultModel updateUsername(@RequestBody AppUserDto appUserDto) {
		appUserService.update(appUserDto);
		return ResultModel.success("修改成功", null);
	}

	/**
	 * 修改手机号
	 * @param appUserDto
	 * @return
	 */
	@ApiOperation(value="修改手机号")
	@ApiOperationSupport(includeParameters = {"appUserDto.phone", "appUserDto.code", "appUserDto.ecode"})
	@PostMapping("updatePhone")
	public ResultModel updatePhone(@RequestBody AppUserDto appUserDto) {
		appUserService.update(appUserDto);
		return ResultModel.success("修改成功", null);
	}

	/**
	 * 修改用户信息
	 * @param appUserDto
	 * @return
	 */
	@ApiOperation(value="修改用户信息")
	@ApiOperationSupport(includeParameters = {"appUserDto.userPhoto", "appUserDto.nickname",
			"appUserDto.sex", "appUserDto.birthday", "appUserDto.signature"})
	@PostMapping("updateUserInfo")
	public ResultModel updateUserInfo(@RequestBody AppUserDto appUserDto) {
		appUserService.update(appUserDto);
		return ResultModel.success("修改成功", null);
	}

	/**
	 * 修改密码
	 * @param updatePwdParams
	 * @return
	 */
	@ApiOperation("修改密码")
	@DynamicParameters(name = "updatePwdParams", properties = {
			@DynamicParameter(name = "oldPwd", value = "旧密码", dataTypeClass = String.class),
			@DynamicParameter(name = "newPwd", value = "新密码", dataTypeClass = String.class)
	})
	@PostMapping("updatePwd")
	public ResultModel updatePwd(@RequestBody Map<String, String> updatePwdParams) {
		String oldPwd = updatePwdParams.get("oldPwd");
		String newPwd = updatePwdParams.get("newPwd");
		String checkOldPwd = updatePwdParams.get("checkOldPwd");
		appUserService.updatePwd(oldPwd, newPwd, checkOldPwd);
		return ResultModel.success("修改成功", null);
	}

	/**
	 * 用户注销
	 * @return
	 */
	@ApiOperation("用户注销")
	@PostMapping("removeUser")
	public ResultModel removeUser() {
		appUserService.removeUser();
		return ResultModel.success("注销成功", null);
	}

}
